<?php
class Query extends CI_Controller {
	
	function Query (){
		parent::__construct();
		$this->load->model(array('user_model', 'activity_model', 'club_model', 'employee_model', 'disability_model', 'institution_model', 'employee_model'));
		if(!($this->session->userdata('id_integrante'))){
			$this->load->helper('url');
			redirect('/home/denied');
		}
	}
	
	function index(){
		$this->load->view('query/index');
	}
	
	function get_club(){
		$this->form_validation->set_rules('club', 'Club', 'required');
		
		$this->form_validation->set_error_delimiters('<div class="error" style="color:#F00; background-color:#FF0;">', '</div>');
		
		if ($this->form_validation->run() == FALSE)
		{
			$data['clubs'] = $this->club_model->all();
			$this->load->view('query/club/get_club', $data);
		}
		else
		{
			$this->club_info($_POST["club"]);		
		}
	}
	
	function club_info($id_club){
		$data['club'] = $this->club_model->find('id_club', $id_club);
		$data['club'] = $data['club'][0];
		$data['club']['institution'] = $this->institution_model->find('id_institucion', $data['club']['ID_INSTITUCION']);
		$data['club']['institution'] = $data['club']['institution'][0];
		$data['club']['employee'] = $this->employee_model->get_employee($data['club']['ID_EMPLEADO_FUNDACION'], 'id_empleado');
		$data['club']['employee'] = $data['club']['employee'][0];
		$data['clubs'] = $this->club_model->all();
		$this->load->view('query/club/get_club', $data);
	}
	
	function get_inst(){
		$this->form_validation->set_rules('inst', 'Inst', 'required');
		
		$this->form_validation->set_error_delimiters('<div class="error" style="color:#F00; background-color:#FF0;">', '</div>');
		
		if ($this->form_validation->run() == FALSE)
		{
			$data['insts'] = $this->institution_model->get_all();
			$this->load->view('query/institution/get_inst', $data);
		}
		else
		{
			$data['inst'] = $this->institution_model->find('id_institucion', $_POST["inst"]);
			$data['inst'] = $data['inst'][0];
			$data['inst']['tipo'] = $this->institution_model->find_type($data['inst']['TIPO_COD_INST_EDU']);
			$data['inst']['tipo'] = $data['inst']['tipo'][0];
			$data['insts'] = $this->institution_model->get_all();
			$this->load->view('query/institution/get_inst', $data);		
		}
	}
	
	function inst_clubs($id_inst){
		$data['clubs'] = $this->club_model->find('ID_INSTITUCION', $id_inst);
		$data['insts'] = $this->institution_model->get_all();
		$data['inst'] = $this->institution_model->find('id_institucion', $id_inst);
		$data['inst'] = $data['inst'][0];
		$this->load->view('query/institution/inst_clubs', $data);
	}
	
	function club_activities($id_club){
		$data['activities'] = $this->club_model->get_club_activites($id_club);			
		for($i=0; $i < count($data['activities']); $i++){
			$data['activities'][$i]['BUDDY'] = $this->user_model->find('id_integrante', $data['activities'][$i]['ID_BEST_BUDDY']);
			$data['activities'][$i]['BUDDY'] = $data['activities'][$i]['BUDDY'][0];
		}
		$data['club'] = $this->club_model->find('id_club', $id_club);
		$data['club'] = $data['club'][0];
		$data['clubs'] = $this->club_model->all();
		$this->load->view('query/club/club_activities', $data);		
		
	}
	
	function paginate_club_members($id){
		$this->load->library('pagination');
		$config['per_page'] = '5';
		$config['base_url'] = base_url().'query/paginate_club_members/'.$id.'/';
		$config['total_rows'] = $this->user_model->get_count_members_by_club($id);
		$config['uri_segment'] = 4; 
		
		$this->pagination->initialize($config);
		
		$data['users'] = $this->user_model->get_paginated_by_club($config['per_page'], $this->uri->segment(4), $id);
		$this->load->vars($data);
				
		$data['clubs'] = $this->club_model->all();
		$this->load->view('query/club/club_members', $data);
	}
	
	function search_user(){
		$this->form_validation->set_rules('text', 'Text', 'required');
		$this->form_validation->set_rules('type', 'Type', 'required');
		
		$this->form_validation->set_error_delimiters('<div class="error" style="color:#F00; background-color:#FF0;">', '</div>');
		
		if ($this->form_validation->run() == FALSE)
		{
			$this->load->view('query/user/search_user');
		}
		else
		{
			$data['users'] = $this->user_model->search($_POST["type"], $_POST["text"]);
			$data['parameters']['type'] = $_POST["type"];
			$data['parameters']['text'] = $_POST["text"];
			$this->load->view('query/user/search_user', $data);	
		}
	}
	
	function get_user($id_user){
		$user = $this->user_model->find('id_integrante', $id_user);
		$data['user'] = $user[0];
		if($data['user']['ID_BEST_BUDDY'] != NULL){
			$data['user']['buddy'] = $this->user_model->find('id_integrante', $data['user']['ID_BEST_BUDDY']);
			$data['user']['buddy'] = $data['user']['buddy'][0];
		}
		if($data['user']['COD_DISCAPACIDAD'] != NULL){
			$data['user']['discapacidad'] = $this->disability_model->find($data['user']['COD_DISCAPACIDAD']);
			$data['user']['discapacidad'] = $data['user']['discapacidad'][0];
		}
		$this->load->view('query/user/get_user', $data);
	}
	
	function user_activities($id_user){
		$data['activities'] = $this->activity_model->get_user_activities($id_user);
		for($i=0; $i<count($data['activities']); $i++){
			$data['activities'][$i]['BUDDY'] = $this->user_model->find('id_integrante', $data['activities'][$i]['ID_BEST_BUDDY']);
			$data['activities'][$i]['BUDDY'] = $data['activities'][$i]['BUDDY'][0];
		}
		$data['buddy_activities'] = $this->activity_model->get_buddy_activities($id_user);
		$data['user'] = $this->user_model->find('id_integrante', $id_user);
		$data['user'] = $data['user'][0];
		$this->load->view('query/user/user_activities', $data);
	}
	
	function user_with_buddy(){
		$this->load->library('pagination');
		$config['per_page'] = '5';
		$config['base_url'] = base_url().'query/user_with_buddy/';
		$config['total_rows'] = $this->user_model->get_count_users_with_buddy();
		$config['total_rows'] = $config['total_rows'][0]['rows'];
		$config['uri_segment'] = 3; 
		
		$this->pagination->initialize($config);
		
		$users = $this->user_model->get_users_with_buddy($config['per_page'], $this->uri->segment(3));
		for($i=0; $i < count($users); $i++){
			$users[$i] = $this->user_model->find('id_integrante', $users[$i]['id_integrante']);
			$users[$i] = $users[$i][0];
			$users[$i]['buddy'] = $this->user_model->find('id_integrante', $users[$i]['ID_BEST_BUDDY']);
			$users[$i]['buddy'] = $users[$i]['buddy'][0];
		}
		$data['users'] = $users;	
		$this->load->vars($data);	
		$this->load->view('query/user/users_with_buddy', $data);
	}
	
	function foundation(){
		$data['foundation'] = $this->employee_model->get_foundation();
		$data['foundation'] = $data['foundation'][0];
		$this->load->view('query/foundation', $data);
	}
	
	
}
?>







